window.onload = function() {
	//for(var k = 0; k < 3; k++) {
	setInterval("createDiv()", 5000);
	//}

	setInterval("mov()", 100);
}

function createDiv() {
	var divs = document.getElementsByTagName("div");
	if(divs.length <= 9) {
		var bodyw = document.documentElement.clientWidth;
		var bodyh = document.documentElement.clientHeight;
		var div = document.createElement("div");
		var x = 0,
			y = 0,
			mx = 10,
			my = 8;
		var x = parseInt(Math.random() * (bodyw - 230));
		var y = parseInt(Math.random() * (bodyh - 230));
		var mx = parseInt(Math.random() * 15);
		var my = parseInt(Math.random() * 15);
		var j = parseInt(Math.random() * 10);
		for(var k = 0; k < j; k++) {
			mx *= -1;
		}
		j = parseInt(Math.random() * 10);
		for(var k = 0; k < j; k++) {
			my *= -1;
		}
		var i = parseInt(Math.random() * 3);
		div.innerHTML = "<img src='imgpaopao/ch" + i + ".png' />";

		div.setAttribute("x", x);
		div.setAttribute("y", y);
		div.setAttribute("mx", mx);
		div.setAttribute("my", my);
		div.style.left = x + "px";
		div.style.top = y + "px";
		document.body.appendChild(div);
	}
}

function mov() {
	var bodyw = document.documentElement.clientWidth;
	var bodyh = document.documentElement.clientHeight;
	var divs = document.getElementsByTagName("div");
	for(var i = 0; i < divs.length; i++) {
		var x = parseInt(divs[i].getAttribute("x"));
		var y = parseInt(divs[i].getAttribute("y"));
		var mx = parseInt(divs[i].getAttribute("mx"));
		var my = parseInt(divs[i].getAttribute("my"));
		if(x + 230 + Math.abs(mx) > bodyw) {
			mx *= -1;
		}
		if(y + 230 + Math.abs(mx) > bodyh) {
			my *= -1;
		}
		if(x + mx < 0) {
			mx *= -1;
		}
		if(y + my < 0) {
			my *= -1;
		}
		x += mx;
		y += my;
		divs[i].style.left = x + "px";
		divs[i].style.top = y + "px";
		divs[i].setAttribute("x", x);
		divs[i].setAttribute("y", y);
		divs[i].setAttribute("mx", mx);
		divs[i].setAttribute("my", my);
	}
}